package cn.com.jsepc.rep.comm;


import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Pattern;


public class tool {
    public tool() {
    }

    public static void LogAdd(String str) {
        if (1 == 1) {
//            System.out.println(str);
        }
    }
    public static String toStringss(String s) {
        return (s == null || s.equals("")) ? "0" : s;
    }
    
    public static String toString_null(String s) {
        return (s == null || s.equals("")|| s.equals("null")) ? "0" : s;
    }
    
    public static String toStringnull(String s) {
        if(s == null || s.equals("")){
        	s="null";
        }else{
        	if(s.trim().equals("")){
        		s="null";
        	}else{
        		s=s.trim();
        		s=numFormat(s);
        		if(s.equals(""))
        		{
        			s="null";
        		}
        	}
        }
        
        
        return s;
    }
    public static String zeroTonull(String s) {
        return (s == null || s.equals("") || Double.parseDouble(s) == 0) ? "" : s;
    }
    public static String nullToZero(String s) {
        return (s == null || s.equals("") || Double.parseDouble(s) == 0) ? "0" : s;
    }
    public static String toStringsss(String s) {
        return (s == null || s.equals("")) ? "1900-01-01" : s;
    }
    public static String toString(String s) {
        return (s == null) ? "- -" : s;
    }
    public static String toStringone(String s) {
        return (s == null || s.equals("null")) ? "" : s.trim();
    }
    public static String toStrings(String s) {
        return (s == null) ? "&nbsp;" : s;
    }
    public static String toString(java.math.BigDecimal s) {
        return (s == null) ? "0" : s.toString();
    }

    public static String toString(java.lang.Integer s) {
        return (s == null) ? "0" : s.toString();
    }

    public static String toString(java.lang.Double s) {
        return (s == null) ? "0.0" : s.toString();
    }
    public static String toString(java.lang.Float s) {
        return (s == null) ? "0.0" : s.toString();
    }
    
    public static String toString_0(String s) {
        return (s == null||s.equals("")) ? "0.0" : s.toString();
    }
    //将空字符串或'null'字符串转为null
    public static String nullStringToNull(String s) {
        return (s == null||s.equals("")||s.equals("null")) ? null : s.toString();
    }
    //由元转为万元
    public static String unitConvert(String price){
		DecimalFormat df = new DecimalFormat("0.0000"); 
		if(null != price && !price.equals("")){
			if (price.endsWith("-")) {
				price = "-"+price.substring(0, price.length()-1);
			}
			price = df.format(Double.parseDouble(price)/10000);
		}
		return price;
	}
    public static String toString_10(String s) {
    	if(s!=null){
    		if(!s.equals("")&&!s.equals("null")){
    			s=Double.toString((Double.parseDouble(s)/10));
    		}else{
    			s="0";
    		}    		
    	}else{
    		s="0";
    	}
        return s;
    }
    
//  解决字符串中分号问题
	public static String filterStr(String str){
		if(str != null){
			if(str.indexOf("'") >= 0) {
				str =  str.replaceAll("'", "");
			}
		}
		if(str != null){
			if(str.indexOf("＇") >= 0) {
				str =  str.replaceAll("＇", "");
			}
		}
		if(str != null){
			if(str.indexOf("’") >= 0) {
				str =  str.replaceAll("’", "");
			}
		}
		return str;
	}
	
	//消除字符串前后空格


	public static String StrTrim(String str) {
		if(str != null) {
			return str.trim();
		}
		return str;
	}
	
    public static String toString(java.sql.Date s) {
    	if(s!=null){   
    		if(!s.equals("")){
    			
    			return s.toString();
    		}else{
    		
    		return  "&nbsp;";
    		}
    	}else{
    		
    		return  "&nbsp;";
    		}
    	
    	
    }

    public static java.util.Vector paserString(String str, String sep) {
        Vector v = new Vector();
        StringTokenizer st = new StringTokenizer(str, sep);
        while (st.hasMoreTokens()) {
            v.addElement((String) st.nextElement());
        }
        return v;
    }
    
    public static ArrayList paserStr(String str, String sep) {
    	ArrayList v = new ArrayList();
        StringTokenizer st = new StringTokenizer(str, sep);
        while (st.hasMoreTokens()) {
            v.add((String) st.nextElement());
        }
        return v;
    }

    //����}λС��
    public static Double getRoundedDouble( double unroundedValue ){
        // Get the integer portion of the value
        double intPortion = Math.floor( unroundedValue );

        // Get the unrounded decimal portion
        double unroundedDecimalPortion = ( unroundedValue - intPortion );


        double roundedDecimalPortion = Math.round( unroundedDecimalPortion * 100 );

        // Shift the decimal point back two places to the right
        roundedDecimalPortion = roundedDecimalPortion / 100;

        // Add the int portion and decimal portions back together
        double total = intPortion + roundedDecimalPortion;
        Double result = Double.valueOf(String.valueOf(total));
        return result;
   }

   //����}λС��
      public static double getRoundeddouble( double unroundedValue ){
          // Get the integer portion of the value
          double intPortion = Math.floor( unroundedValue );

          // Get the unrounded decimal portion
          double unroundedDecimalPortion = ( unroundedValue - intPortion );


          double roundedDecimalPortion = Math.round( unroundedDecimalPortion * 100 );

          // Shift the decimal point back two places to the right
          roundedDecimalPortion = roundedDecimalPortion / 100;

          // Add the int portion and decimal portions back together
          double total = intPortion + roundedDecimalPortion;
         // Double result = Double.valueOf(String.valueOf(total));
          return total;
   }
   //������λС��
    public static Double getRoundedDouble3( double unroundedValue ){
        // Get the integer portion of the value
        double intPortion = Math.floor( unroundedValue );

        // Get the unrounded decimal portion
        double unroundedDecimalPortion = ( unroundedValue - intPortion );


        double roundedDecimalPortion = Math.round( unroundedDecimalPortion * 1000 );

        // Shift the decimal point back two places to the right
        roundedDecimalPortion = roundedDecimalPortion / 1000;

        // Add the int portion and decimal portions back together
        double total = intPortion + roundedDecimalPortion;

        Double result = Double.valueOf(String.valueOf(total));
        return result;
   }

   //������λС��
    public static Double getRoundedDouble4( double unroundedValue ){
        // Get the integer portion of the value

        double intPortion = Math.floor( unroundedValue );

        // Get the unrounded decimal portion
        double unroundedDecimalPortion = ( unroundedValue - intPortion );


        double roundedDecimalPortion = Math.round( unroundedDecimalPortion * 10000 );

        // Shift the decimal point back two places to the right
        roundedDecimalPortion = roundedDecimalPortion / 10000;

        // Add the int portion and decimal portions back together
        double total = intPortion + roundedDecimalPortion;

        Double result = Double.valueOf(String.valueOf(total));
        return result;
   }
    
    public static List getSplitStr(String str,int sub){
    	List lstStr = new ArrayList();
		if (str==null || str.equals("") || sub==0){
			return lstStr;
		}
		
		//2010-11-29 修改 去除字段中的特殊符号
		str = str.replaceAll("	", ""); //制表符

		str = str.replaceAll("'", "");	//左单引号（英）

		str = str.replaceAll("'", "");	//右单引号（英）

		str = str.replaceAll("‘", "");	//左单引号（中）

		str = str.replaceAll("’", "");	//右单引号（中）

		
		str = str.replaceAll("\"", "");	//左双引号（英）

		str = str.replaceAll("\"", "");	//右双引号（英）

		str = str.replaceAll("“", "");	//左双引号（中）

		str = str.replaceAll("”", "");	//右双引号（中）

		
		str = str.replaceAll("\r", "");	//换行符

		str = str.replaceAll("\n", "<br>&nbsp;&nbsp;&nbsp;&nbsp;");	//确定键

		
    	int length = str.length();
    	int sz = 0;
    	int end = 0;
    	if( length % sub != 0){
    		sz = length / sub + 1;
    	}else{
    		sz = length / sub;
    	}    	
		for(int i=0;i < sz ;i++){
    		if(i == sz-1){
    			end = length;
    		}else{
    			end = (i+1)*sub;
    		}
    		lstStr.add(str.substring(i*sub,end));
    	}
		return lstStr;
    }
    
    public static String getSplitStr2(String str,int sub){
    	String ret ="";
		if (str==null || str.equals("") || sub==0){
			return ret;
		}

		//2010-11-29 修改 去除字段中的特殊符号
		str = str.replaceAll("	", ""); //制表符

		str = str.replaceAll("'", "");	//左单引号（英）

		str = str.replaceAll("'", "");	//右单引号（英）

		str = str.replaceAll("‘", "");	//左单引号（中）

		str = str.replaceAll("’", "");	//右单引号（中）

		
		str = str.replaceAll("\"", "");	//左双引号（英）

		str = str.replaceAll("\"", "");	//右双引号（英）

		str = str.replaceAll("“", "");	//左双引号（中）

		str = str.replaceAll("”", "");	//右双引号（中）

		
		str = str.replaceAll("\r", "");	//换行符

		str = str.replaceAll("\n", "<br>&nbsp;&nbsp;&nbsp;&nbsp;");	//确定键

		
    	int length = str.length();
    	if(length<=sub){
    		ret = str;
    	} else{
    		ret = str.substring(0, sub);
    	}
    	return ret;
    }
    
    public static String getReplaceStr(String str){
		if (str==null || str.equals("")){
			return "";
		}
		str = str.replaceAll("	", ""); //制表符

		str = str.replaceAll("'", "");	//左单引号（英）

		str = str.replaceAll("'", "");	//右单引号（英）

		str = str.replaceAll("‘", "");	//左单引号（中）

		str = str.replaceAll("’", "");	//右单引号（中）

		
		str = str.replaceAll("\"", "");	//左双引号（英）

		str = str.replaceAll("\"", "");	//右双引号（英）

		str = str.replaceAll("“", "");	//左双引号（中）

		str = str.replaceAll("”", "");	//右双引号（中）

		
		str = str.replaceAll("\r", "");	//换行符

		str = str.replaceAll("\n", "");	//确定键
		
		str = str.replaceAll("<br>", "");	//
		
		str = str.replaceAll("&nbsp;", "");	//
		
		str = str.replaceAll(" ", "");	//

    	return str;
    }
    
    public static String getLst2Str(List lst){
		StringBuffer sb = new StringBuffer();
		if (lst != null){
			int iLen = lst.size();
			for (int i=0;i<iLen;i++){
				if (i == (iLen - 1)){
					sb.append("'"+lst.get(i)+"'");
				}else{
					sb.append("'"+lst.get(i)+"',");
				}
			}
			
		}
		return sb.toString();
	}

    public static ArrayList getHt2Lst(Hashtable ht){
		ArrayList arlst = new ArrayList();
		if (ht != null){
			for (Iterator i = ht.values().iterator();i.hasNext();){
				arlst.add((String)i.next());
			}
		}
		return arlst;
	}
    
    public static double getDivision(double num1,double num2)
    {
    	if(num1==0||num2==0)
    	{
    		return 0.0;
    	}
    	else
    		return num1/num2;
    }
    
    public static String sameLen(int p, int l) throws Exception {
		String s = String.valueOf(p);
		if (l < s.length())
			return s.substring(0, l);
		else {
			String r = "";
			for (int i = 0; i < l - s.length(); i++)
				r += "0";
			r += s;
			return r;
		}
	}
    
    public static String sameLen(String s, int l) throws Exception {
		if (s == null){
			s = "";
		}
    	if (l < s.length())
			return s.substring(0, l);
		else {
			StringBuffer r = new StringBuffer();
			for (int i = 0; i < l - s.length(); i++){
				r.append("0");
			}
			r.append(s);
			return r.toString();
		}
	}
	//去除小数点后末位0的问题，如18.10，方法返回18.1;若17.00，返回17
	public static String filterNumber(String num) {
		String filtered = "";
		if(num != null) {								//确保num不为空


			if(num.matches("\\w*[.]\\w*[0]")) {			//确保num为小数


				int i = num.length()-1;					
				while(num.charAt(i) == '0' && num.charAt(i) != '.' && i > 0) {			//循环去掉最后一位0
					num = num.substring(0, i);
					i --;
				}
				if(num.charAt(num.length()-1) == '.') {
					num = num.substring(0, num.length()-1);
				}
			}
			if(num.equals("0")) {
				num = "";
			}
			filtered = num;
		}
		return filtered;
	}
//	去除小数点后末位0的问题，如18.10，方法返回18.1;若17.00，返回17
	public static String filterNum(String num) {
		String filtered = "";
		if(num != null) {								//确保num不为空


			if(num.matches("\\w*[.]\\w*[0]")) {			//确保num为小数


				int i = num.length()-1;					
				while(num.charAt(i) == '0' && num.charAt(i) != '.' && i > 0) {			//循环去掉最后一位0
					num = num.substring(0, i);
					i --;
				}
				if(num.charAt(num.length()-1) == '.') {
					num = num.substring(0, num.length()-1);
				}
			}
			filtered = num;
		}
		return filtered;
	}
	
//  解决字符串中分号问题
	public static String fileStr(String str){
		if(str != null){
			if(str.indexOf(",") >= 0) {
				str =  str.replaceAll(",", "','");
			}
		}
		return str;
	}
	
	
//  只能数字与"."
	public static String numFormat(String str){
		String ss="";
		String sss="";
		String ttt="";
		if(str != null){
			int t=str.length();
			if(t>0){
				for(int i=0;i<t;i++){
					ss=str.substring(i,i+1);
					if(ss.indexOf(".")!=-1||ss.indexOf("0")!=-1||ss.indexOf("1")!=-1||ss.indexOf("2")!=-1||ss.indexOf("3")!=-1||ss.indexOf("4")!=-1||ss.indexOf("5")!=-1||ss.indexOf("6")!=-1||ss.indexOf("7")!=-1||ss.indexOf("8")!=-1||ss.indexOf("9")!=-1){						
					}else{
						ss="";
					}
					sss=sss+ss;
				}
			}
//		    int dian=sss.indexOf(".");
//			ttt=sss.substring(dian+1,sss.length());
//			String w="";
//			String ww="";
//			if(ttt.indexOf(".")>=0){
//				for(int i=0;i<ttt.length();i++){
//					w=ttt.substring(i,i+1);
//					if(w.indexOf("0")!=-1||w.indexOf("1")!=-1||w.indexOf("2")!=-1||w.indexOf("3")!=-1||w.indexOf("4")!=-1||w.indexOf("5")!=-1||w.indexOf("6")!=-1||w.indexOf("7")!=-1||w.indexOf("8")!=-1||w.indexOf("9")!=-1){						
//					}else{
//						w="";
//					}
//					ww=ww+w;
//				}
//			}
//			sss=sss.substring(0,dian+1)+ww;
		}
		return sss;
	}
	
	public static boolean isInteger(String str){
		if(str == null || str.equals(""))
			return false;
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();   
	}
	
	public static boolean isDecimal(String str){
		if(str == null || str.equals(""))
			return false;
		Pattern pattern = Pattern.compile("[0-9]*.[0-9]*");
		return pattern.matcher(str).matches();   
	}
	
	public static boolean isDecimal2(String str){
		if(str == null || str.equals(""))
			return false;
		Pattern pattern = Pattern.compile("[0-9]{0,2}.[0-9]*");
		boolean isnum = pattern.matcher(str).matches();   
		if(isnum){
			double db = Double.parseDouble(str);
			if(db >= 0 && db <=100){
				return true;
			}else{
				return false;
			}
		}else{
			return isnum;
		}
	}
	
	public static boolean isInteger2(String str){
		if(str == null || str.equals(""))
			return false;
		Pattern pattern = Pattern.compile("[0-9]{4}");
		return pattern.matcher(str).matches();   
	}

	
}
